package checkers.gui.dialogs;

import checkers.ia.iaPlayer;
import checkers.model.Player;
import checkers.model.Player.Color;
import checkers.common.Settings;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

/**
 * Class NewGameDialog
 * Show start new game dialog
 * 
 * @author Cristian Tardivo
 */
public class NewGameDialog extends javax.swing.JDialog {
    private static final ResourceBundle lang = ResourceBundle.getBundle("checkers/common/lang");
    private boolean result;
    
    
    /**
     * Create new NewGameDialog
     * @param parent Parent Frame
     * @param modal Modal exclude
     */
    public NewGameDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        // Set default data
        Player1_name.setText(Settings.getNamePlayerA());
        Player2_name.setText(Settings.getNamePlayerB());
        if(Settings.getColorPlayerA().equals(Color.red)){
            Red_1.setSelected(true);
            Black_2.setSelected(true);
        } else {
            Black_1.setSelected(true);
            Red_2.setSelected(true);
        }
        cpu_player1.setSelected(Settings.getTypePlayerA() == Player.Type.artificial);
        cpu_player2.setSelected(Settings.getTypePlayerB() == Player.Type.artificial);
        easy_butt.setSelected(Settings.getDifficult() == iaPlayer.Difficult.easy);
        moderate_butt.setSelected(Settings.getDifficult() == iaPlayer.Difficult.moderate);
        hard_butt.setSelected(Settings.getDifficult() == iaPlayer.Difficult.hard);
        this.getRootPane().setDefaultButton(buttonAcept);
        // Key ESCAPE action
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction(){public void actionPerformed(ActionEvent e){buttonCancel.doClick();}}; 
        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        this.getRootPane().getActionMap().put("ESCAPE", escapeAction);
    }
    
    /**
     * Get Start Result
     * @return 
     */
    public boolean getResult(){
        return result;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        colorGroup1 = new javax.swing.ButtonGroup();
        colorGroup2 = new javax.swing.ButtonGroup();
        diffGroup = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        Player1_name = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        Red_1 = new javax.swing.JRadioButton();
        Black_1 = new javax.swing.JRadioButton();
        cpu_player1 = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cpu_player2 = new javax.swing.JCheckBox();
        Player2_name = new javax.swing.JTextField();
        Red_2 = new javax.swing.JRadioButton();
        Black_2 = new javax.swing.JRadioButton();
        buttonAcept = new javax.swing.JButton();
        buttonCancel = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        easy_butt = new javax.swing.JRadioButton();
        moderate_butt = new javax.swing.JRadioButton();
        hard_butt = new javax.swing.JRadioButton();

        colorGroup1.add(this.Black_1);
        colorGroup1.add(this.Red_1);

        colorGroup2.add(this.Black_2);
        colorGroup2.add(this.Red_2);

        diffGroup.add(easy_butt);
        diffGroup.add(moderate_butt);
        diffGroup.add(hard_butt);

        setTitle(lang.getString("START_NEW_GAME")); // NOI18N
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(lang.getString("PLAYER_1"))); // NOI18N

        jLabel1.setText(lang.getString("NAME")); // NOI18N

        Player1_name.setText(lang.getString("PLAYER_A")); // NOI18N

        jLabel2.setText(lang.getString("COLOR")); // NOI18N

        Red_1.setSelected(true);
        Red_1.setText(lang.getString("RED")); // NOI18N
        Red_1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Red_1ActionPerformed(evt);
            }
        });

        Black_1.setText(lang.getString("BLACK")); // NOI18N
        Black_1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Black_1ActionPerformed(evt);
            }
        });

        cpu_player1.setText(lang.getString("ARTIFICIAL")); // NOI18N
        cpu_player1.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Red_1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Black_1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Player1_name, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(cpu_player1)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Player1_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(Red_1)
                    .addComponent(Black_1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 2, Short.MAX_VALUE)
                .addComponent(cpu_player1))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(lang.getString("PLAYER_2"))); // NOI18N

        jLabel3.setText(lang.getString("NAME")); // NOI18N

        jLabel4.setText(lang.getString("COLOR")); // NOI18N

        cpu_player2.setText(lang.getString("ARTIFICIAL")); // NOI18N
        cpu_player2.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);

        Player2_name.setText(lang.getString("PLAYER_B")); // NOI18N

        Red_2.setText(lang.getString("RED")); // NOI18N
        Red_2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Red_2ActionPerformed(evt);
            }
        });

        Black_2.setSelected(true);
        Black_2.setText(lang.getString("BLACK")); // NOI18N
        Black_2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Black_2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(cpu_player2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(Red_2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Black_2))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Player2_name)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(Player2_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(Red_2)
                    .addComponent(Black_2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cpu_player2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonAcept.setText(lang.getString("ACCEPT")); // NOI18N
        buttonAcept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAceptActionPerformed(evt);
            }
        });

        buttonCancel.setText(lang.getString("CANCEL")); // NOI18N
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(lang.getString("DIFFICULTY"))); // NOI18N

        easy_butt.setSelected(true);
        easy_butt.setText(lang.getString("EASY")); // NOI18N

        moderate_butt.setText(lang.getString("MODERATE")); // NOI18N

        hard_butt.setText(lang.getString("HARD")); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(easy_butt)
                .addGap(18, 18, 18)
                .addComponent(moderate_butt)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(hard_butt)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(moderate_butt)
                .addComponent(hard_butt)
                .addComponent(easy_butt))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(buttonAcept)
                        .addGap(18, 18, 18)
                        .addComponent(buttonCancel)
                        .addGap(16, 16, 16))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(12, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonAcept)
                    .addComponent(buttonCancel))
                .addGap(10, 10, 10))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Red_1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Red_1ActionPerformed
        Black_2.setSelected(true);
    }//GEN-LAST:event_Red_1ActionPerformed

    private void Black_1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Black_1ActionPerformed
        Red_2.setSelected(true);
    }//GEN-LAST:event_Black_1ActionPerformed

    private void Red_2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Red_2ActionPerformed
        Black_1.setSelected(true);
    }//GEN-LAST:event_Red_2ActionPerformed

    private void Black_2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Black_2ActionPerformed
        Red_1.setSelected(true);
    }//GEN-LAST:event_Black_2ActionPerformed

    /**
     * Save Game Data
     * @param evt 
     */
    private void buttonAceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAceptActionPerformed
        if(Player1_name.getText().isEmpty()){
            Player1_name.requestFocus();
            return;
        }
        if(Player2_name.getText().isEmpty()){
            Player2_name.requestFocus();
            return;
        }        
        Settings.setNamePlayerA(Player1_name.getText());
        Settings.setNamePlayerB(Player2_name.getText());
        if(Red_1.isSelected()){
            Settings.setColorPlayerA(Player.Color.red);
            Settings.setColorPlayerB(Player.Color.black);
        } else {
            Settings.setColorPlayerA(Player.Color.black);
            Settings.setColorPlayerB(Player.Color.red);
        }
        Settings.setTypePlayerA(cpu_player1.isSelected()?Player.Type.artificial:Player.Type.local);
        Settings.setTypePlayerB(cpu_player2.isSelected()?Player.Type.artificial:Player.Type.local);
        if(easy_butt.isSelected())
            Settings.setDifficult(iaPlayer.Difficult.easy);
        if(moderate_butt.isSelected())
            Settings.setDifficult(iaPlayer.Difficult.moderate);
        if(hard_butt.isSelected())
            Settings.setDifficult(iaPlayer.Difficult.hard);
        result = true;
        dispose();
    }//GEN-LAST:event_buttonAceptActionPerformed

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        result = false;
        dispose();
    }//GEN-LAST:event_buttonCancelActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton Black_1;
    private javax.swing.JRadioButton Black_2;
    private javax.swing.JTextField Player1_name;
    private javax.swing.JTextField Player2_name;
    private javax.swing.JRadioButton Red_1;
    private javax.swing.JRadioButton Red_2;
    private javax.swing.JButton buttonAcept;
    private javax.swing.JButton buttonCancel;
    private javax.swing.ButtonGroup colorGroup1;
    private javax.swing.ButtonGroup colorGroup2;
    private javax.swing.JCheckBox cpu_player1;
    private javax.swing.JCheckBox cpu_player2;
    private javax.swing.ButtonGroup diffGroup;
    private javax.swing.JRadioButton easy_butt;
    private javax.swing.JRadioButton hard_butt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JRadioButton moderate_butt;
    // End of variables declaration//GEN-END:variables
}
